package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

//    新增地址
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        //正常添加新增地址用户的信息+设置user中id设置到addressBook中user-id
        //获取user的id  BaseContext.getCurrentId()获取当前登录者的id
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("新增地址成功！！！");
    }
    //设置默认地址
    @PutMapping("/default")
     public R<String> defauletaddress(@RequestBody AddressBook addressBook){
        //将所有的is_default全部改成0
//        update address_book set is_default = 0 where  user_id =  ?
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AddressBook::getIsDefault,0);
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        addressBookService.update(wrapper);
        //将设置默认地址isdafult改为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success("设置默认地址成功！！！");
    }

  //根据id查询地址
    @GetMapping("/{id}")
    public R<AddressBook> selectid(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    //查询默认地址
    @GetMapping("/default")
    public R<AddressBook> seletcedefault(){
        //条件构造器找到addressBook中isdefault为1+addressBook中的user_id与user id相等
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        //条件判断看有无addressbook对象
        if(addressBook==null){
            return R.success(null);
        }
        return R.success(addressBook);
    }
    //查询指定用户的全部地址
    @GetMapping("/list")
    public R<List> selecetAll(){
        //查找出所有addressbook中的id与user id相等的情况
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    //删除一个或者多个地址
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除地址成功！！");
    }

    //修改地址
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功！！！");
    }
}
